package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.WorkFixtures;
import org.example.erp.service.WorkFixturesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class WorkFixtureController {
    @Autowired
    WorkFixturesService workFixturesService;

    @GetMapping("/fixtureLedger/find")
    public DataResult find(
            @RequestParam(value = "fixtureCode") String fixtureCode,
            @RequestParam(value = "fixtureName") String fixtureName,
            @RequestParam(value = "typeCode") String typeCode,
            @RequestParam(value = "brand") String brand,
            @RequestParam(value = "model") String model,
            @RequestParam(value = "status") String status,
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<WorkFixtures> page = new Page<>(currentPage, pageSize);
        QueryWrapper<WorkFixtures> queryWrapper = new QueryWrapper();
        if (fixtureCode != null && !"".equals(fixtureCode)) {
            queryWrapper.lambda().like(WorkFixtures::getFixtureCode, fixtureCode);
        }
        if (fixtureName != null && !"".equals(fixtureName)) {
            queryWrapper.lambda().like(WorkFixtures::getFixtureName, fixtureName);
        }
        if (typeCode != null && !"".equals(typeCode)) {
            queryWrapper.like("w.type_code", typeCode);
        }
        if (brand != null && !"".equals(brand)) {
            queryWrapper.lambda().like(WorkFixtures::getBrand, brand);
        }
        if (model != null && !"".equals(model)) {
            queryWrapper.lambda().like(WorkFixtures::getModel, model);
        }
        if (status != null && !"".equals(status)) {
            queryWrapper.lambda().like(WorkFixtures::getStatus, status);
        }
        page = workFixturesService.getTypeAndFixtures(page, queryWrapper);
        return DataResult.success(page);
    }
    @GetMapping("/fixtureLedger/findById")
    public DataResult findById(@RequestParam(value = "id") String id) {
        WorkFixtures workFixtures = workFixturesService.getById(id);
        return DataResult.success(workFixtures);
    }

    @PostMapping("/fixtureLedger/insert")
    public DataResult insert(@RequestBody WorkFixtures workFixtures) {
        boolean save = workFixturesService.save(workFixtures);
        return DataResult.success(save?"成功":"失败");
    }
    @DeleteMapping("/fixtureLedger/delete")
    public DataResult delete(@RequestBody List<String> id) {
        boolean removeById = workFixturesService.removeByIds(id);
        return DataResult.success(removeById?"成功":"失败");
    }

    @PutMapping("/fixtureLedger/update")
    public DataResult update(@RequestBody WorkFixtures workFixtures) {
        boolean update = workFixturesService.updateById(workFixtures);
        return DataResult.success(update?"成功":"失败");
    }
}
